<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1,minimum-scale=1, maximum-scale=1, user-scalable=no">

    <title>震动测试</title>
    <style>
        * {
            moz-user-select: -moz-none;
            -moz-user-select: none;
            -o-user-select: none;
            -khtml-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
    </style>
</head>

<body>
<h1 onclick="navigator.vibrate([300,200,300,200])">点我震动</h1>
<a href="tel:15532491061">打电话</a>
<a href="sms:15532491061?body=我是二狗子收到请回答">发短信</a>
<script>
    if (window.DeviceMotionEvent) {
        window.addEventListener('devicemotion', deviceMotionHandler, false);
    }
    var vibrateSupport = "vibrate" in navigator
    if (vibrateSupport) { //兼容不同的浏览器
        navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
    }

    function deviceMotionHandler(eventData) {
        var acceleration = eventData.accelerationIncludingGravity;
        var currTime = new Date().getTime();
        var diffTime = currTime - last_update;
        console.info(diffTime);
        if (diffTime > 100) {
            last_update = currTime;
            x = acceleration.x;
            y = acceleration.y;
            z = acceleration.z;
            var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 30000;
            console.info(speed);
            if (speed > SHAKE_THRESHOLD) {
                //要一摇之后进行业务逻辑处理
                alert("huangle")
                doResult();

            }
            last_x = x;
            last_y = y;
            last_z = z;

        }
    }
</script>

</body>
</html>